package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.Util.FileSize;
import com.zhao.Util.HttpContextUtil;
import com.zhao.Util.IPUtil;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Dataup;
import com.zhao.pojo.User;
import com.zhao.service.DataupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
@RequestMapping("/data")
public class DataController {

    @Autowired
    DataupService dataupService;
    @RequestMapping("/toData")
    public String toData(){
        return "admin/dataCenter";
    }
    @RequestMapping("/fen")
    @ResponseBody
    public Map<String,Object> fen(@RequestParam(defaultValue="1")int page, @RequestParam(defaultValue="5")int limit){
        Page<Dataup> page1 = new Page<>(page,limit);
        IPage<Dataup> iPage = dataupService.page(page1, null);
        Map<String,Object> map=new HashMap<>();		//装填回调数据
        map.put("code", 0);
        map.put("msg", "随便写啥");
        map.put("count",iPage.getTotal());		//获得数据的总数
        map.put("data",iPage.getRecords());	//本次分页查询的数据
        System.out.println("===>123");
        return map;
    }
    //@MyLog(value = "手动数据备份")
    @ResponseBody
    @RequestMapping("/newData")
    public String newData() throws Exception {
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String ip= IPUtil.getIpAddr(request);
        if (ip.equals("本地"))
            ip="localhost";
        //获取存储位置
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());

        String rootPath =System.getProperty("user.dir")+"/upload/Mysql/";

        // 新文件名
        String newFileName = "Mysql" + res +".sql";
        // 创建年月文件夹
        Calendar date = Calendar.getInstance();
        File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));

        // 新文件
        File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
        // 判断目标文件所在目录是否存在
        if( !newFile.getParentFile().exists()) {
            // 如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        //记录
        String cmd = "cmd /c mysqldump -h"+ip+" -p3306 -uroot -px5 blogs>"+newFile.getPath();
        Process process = Runtime.getRuntime().exec(cmd);//执行语句
        process.waitFor();//等待上述命令执行完毕后打印下面log
        log.info("数据库备份结束，备份结果：{}",process.exitValue() == 0 ? "success" : "fail");
        //存储到数据库
        Dataup dataup = new Dataup();
        dataup.setSqlname(newFileName);
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        dataup.setOperator(loginUser.getUserAccount());
        dataup.setAbsolutepath(newFile.getPath());
        dataupService.save(dataup);
        return "1";
    }
    //@MyLog(value = "删除数据备份")
    @ResponseBody
    @RequestMapping("/del/{id}")
    public String del(@PathVariable Integer id){
        dataupService.removeById(id);
        return "1";
    }
}
